/* Character mask type.

   This file is part of khipu.

   khipu is free software: you can redistribute it and/or modify
   it under the terms of the GNU Lesser General Public License as published by
   the Free Software Foundation; either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public License
   along with this program.  If not, see <https://www.gnu.org/licenses/>.  */

#ifndef __KP_UTILS_CHMASK__
#define __KP_UTILS_CHMASK__   1

#include "../defs.hpp"

KP_DECLS_BEGIN

struct chmask
{
  uint32_t mask[8];

  chmask ()
    {
      this->reset ();
    }

  chmask (const char *s)
    {
      this->reset ();
      for (; *s != '\0'; ++s)
        this->set ((uint8_t)*s);
    }

  chmask (const char *s, int n)
    {
      this->reset ();
      for (int i = 0; i < n; ++i)
        this->set ((uint8_t)s[i]);
    }

  void reset ()
    {
      for (size_t i = 0; i < KP_NELEM (this->mask); ++i)
        this->mask[i] = 0;
    }

  void set (int ch)
    {
      this->mask[ch / 32] |= 1u << (ch % 32);
    }

  void clr (int ch)
    {
      this->mask[ch / 32] &= ~(1u << (ch % 32));
    }

  bool tst (int ch)
    {
      return ((this->mask[ch / 32] & (1u << (ch % 32))) != 0);
    }
};

KP_DECLS_END

#endif
